#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   leqing.py
@Time    :   2024/01/16 16:11:41
@Author  :   chakcy 
@Email   :   947105045@qq.com
@description   :   乐清爬虫类
'''

from pyquery import PyQuery as pq

import re
import os
import sys
sys.path.append("../../")

from reptile import Reptile
from excel import Excel

# 乐清网站爬虫类
class LeQing(Reptile):
    # 初始化
    def __init__(self, dataPath:str, driver_path):
        url = "https://www.yqrc.com/personal/login"
        list_template = "https://www.yqrc.com/job?keyword={}&keyword_type=1&map_show=0&page={}"
        sql_table = """
            create table if not exists leqing(
                id integer,
                jobname varchar(50),
                number varchar(50),
                sex varchar(50),
                company varchar(50),
                phone varchar(50),
                pensonName varchar(50),
                address varchar(100),
                date timestamp not null default (datetime('now', 'localtime')),
                UNIQUE (jobname, company),
                primary key (id)
                );
            """
        super().__init__(url, dataPath, list_template, sql_table, driver_path)

    # 模拟登录
    def login(self, username:str, password:str):
        usernameDict = {"selector":"#username", "value":username}
        passwordDict = {"selector":"#password", "value":password}
        loginBtn = "#sub"
        return super().login(usernameDict, passwordDict, loginBtn)
    
    # 获取url
    def getUrl(self, key: str, pages: int):
        return super().getUrl(key, pages)

    # 执行函数
    def run(self):
        return super().run()
    
    # 保存数据
    def save(self):
        sql = """
            replace 
            into 
                leqing (
                    jobname,
                    number,
                    sex,
                    company,
                    phone,
                    pensonName,
                    address)
            values(?, ?, ?, ?, ?, ?, ?)
        """
        return super().save(sql)
    
    # 关闭爬虫
    def close(self):
        return super().close()
    
    # 解析页面提取链接
    def parseUrl(self, element: pq):
        message_url_list = []
        for i in element('.jobs-item').items():
            message_url_list.append(i.find('.jobs-item__main a').attr('href'))
        return message_url_list

    # 解析页面提取数据
    def analysis(self, element: pq):
        dataDict = {}
        dataDict["jobname"] = element('.left-title h1 span').text()
        if dataDict["jobname"] == "":
            dataDict["jobname"] = element('.col-xs-7 .name h1').text()
            for i, item in enumerate(element('.col-xs-4').items()):
                if i == 1:
                    dataDict["number"] = item('.value').text()
                if i == 3:
                    dataDict["sex"] = item('.value').text()
            dataDict["company"] = element('h1 a').attr('title')
            dataDict["phone"] = element('.view-contacts #view-phone').text()
            dataDict["pensonName"] = element('.job-publish .box .row .col-xs-6 .name .g-left strong').text()
            dataDict["address"] = element('.col-xs-12.hover-icon .value').text()

        else:
            dataDict["number"] = element('.ico2~em').text()
            dataDict["sex"] = element('.ico5~em').text()
            dataDict["company"] = element('.titleLink').attr('title')
            dataDict["phone"] = element('.f-14 strong').text()
            dataDict["pensonName"] = element('.publisher_info .info .advisory h3').text()
            dataDict["address"] = element('.adress em').text()
        
        if len(dataDict) < 7:
            return {}

        return dataDict

# 数据导出类
class LeQingTOExcel(Excel):
    # 初始化
    def __init__(self, path, db_name, table_name):
        super().__init__(path, db_name, table_name, 1)

    # 将所有数据导出成Excel
    def dataToExcelAll(self):
        return super().dataToExcelAll()

    # 将指定数据导出成Excel
    def dataToExcel(self):
        columns = {"jobname":"岗位","number":"人数要求","sex":"性别限制","company":"公司名称","phone":"联系电话","pensonName":"联系人名字","address":"公司地址"}
        df = self.df.rename(columns=columns)
        df = df[df['人数要求'] != '若干名']
        df['人数要求'] = df['人数要求'].apply(lambda x:int(re.findall(r"\d+\.*?",x)[0]))
        df = df[df["人数要求"] > 20]
        excel_file_path = os.path.join(self.path, f"{self.db_name.split('.')[0]}.xlsx")
        super().excelSave(excel_file_path, df)


if __name__ == "__main__":
    leqing = LeQing(dataPath="pugong.db")
    leqing.login(username="######", password="######")
    leqing.getUrl("普工", 1)
    leqing.run()
    leqing.save()
    leqing.close()
    to_excel = LeQingTOExcel("./", "pugong.db", "leqing")
    to_excel.dataToExcel()
    to_excel.dataToExcelAll()
    